<?php
	$goto = isset($_SESSION['return_path']) ? $_SESSION['return_path'] : $path.$table;
	$redirect = true;

	if (isset($_POST['amount'])) {
		$amount = $_POST['amount'];
		$_SESSION['purchase_days'] = $amount;
		$now = date("Y-m-d H:i:s");
		$res = $cn->Data('select startdate, enddate, subscriptiontypeid from subscriptions where userid='.$user_id, false);
		
		if (sizeof($res) > 0) {
			if ($res[0]['enddate'] == $res[0]['startdate'] || $res[0]['enddate'] > $now) {
				$redirect = false;
				$form = false;
				$res = $cn->Data('select dailyrate from subscription_types where id='.$res[0]['subscriptiontypeid'], false);
				$price = $res[0]['dailyrate'];

				$purchase = $price * $amount;

				// create invoice, get invoice number add as reference.
				$inv = $cn->Data('select id from invoices order by id desc limit 1', false);
				if (sizeof($inv) == 0) $nextid = 1;
				else $nextid = intval($inv[0]['id']) + 1;
				$inv = null;			
				$invno = 'SCR'.date('Ym').str_pad($nextid, 4, '0', STR_PAD_LEFT);

				$res = $cn->Add('invoices', array('date'=>$now, 'userid'=>$user_id, 'label'=>$invno, 'amount'=>$purchase, 'content'=>'blank for now', 'paid'=>0, 'completed'=>0), false);

				// create a log of transaction
				$res = $cn->Add('logs', array('date'=>$now, 'userid'=>$user_id, 'description'=>'Purchase Sett Credit pending. Invoice ['.$invno.']', 'type'=>'credit'), false);

				$user_email = $userdata['email'];
				$return_url = 'http://'.$server.$path.$table.'/confirm';
				$purchase = $purchase.'00';
				$secretKey = md5("10011013800|$invno|$purchase|ZAR|$return_url|$now|$user_email|secret");//PAYGATE_ID|REFERENCE|AMOUNT|CURRENCY|RETURN_URL|TRANSACTION_DATE|EMAIL|KEY

				$str = '<h2>Confirm Purchase</h2>
				<h3>Amount due: R'.($price * $amount).'</h3>
				<ul class="details grey">
					<li>Days</li><li>'.$amount.'</li>
					<li>Price</li><li>'.$price.'</li>
				</ul>
				<p>You are about to be redirect to a secure <a href="http://www.paygate.co.za" target="_blank">PayGate</a> page to complete this transaction.<br/>On completition of the transaction, using your prefered payment method, you will be returned to your dashboard.</p>
				<p>Please note: The Sett does not store any payment details. All transactions are handled using <a href="http://www.paygate.co.za" target="_blank">PayGate</a>.</p>
				<form action="https://www.paygate.co.za/paywebv2/process.trans" method="POST">
				<input type="hidden" name="PAYGATE_ID" value="10011013800">
				<input type="hidden" name="REFERENCE" value="'.$invno.'">
				<input type="hidden" name="AMOUNT" value="'.$purchase.'">
				<input type="hidden" name="CURRENCY" value="ZAR">
				<input type="hidden" name="RETURN_URL" value="'.$return_url.'">
				<input type="hidden" name="TRANSACTION_DATE" value="'.$now.'">
				<input type="hidden" name="EMAIL" value="'.$user_email.'">
				<input type="hidden" name="CHECKSUM" value="'.$secretKey.'">
				<input type="submit" value="Proceed" class="button" id="PayGateSubmit">
				</form>';
				echo $str;
			}
		}
	}

	if ($redirect) header('location: '.$goto);
?>